//#include <stdio.h>
//
//int main(void) 
//{
//    int n;
//    if (scanf_s("%d", &n) != 1) return 0;
//
//    int a[100000];          
//    for (int i = 0; i < n; ++i) scanf_s("%d", &a[i]);
//
//    int maxLen = 1;     
//    int cur = 1;         
//
//    for (int i = 1; i < n; ++i) 
//    {
//        if (a[i] - a[i - 1] <= 1 && a[i] - a[i - 1] >= -1)
//            ++cur;
//        else 
//        {
//            if (cur > maxLen) maxLen = cur;
//            cur = 1;
//        }
//    }
//    if (cur > maxLen) maxLen = cur;  
//    printf("%d\n", maxLen);
//    return 0;
//}

//#include <stdio.h>
//#include <stdlib.h>
//
//int cmp_int(const void* p1, const void* p2) 
//{
//    int a = *(const int*)p1;
//    int b = *(const int*)p2;
//    return (a > b) - (a < b);
//}
//
//int main(void)
//{
//    int n, k;
//    if (scanf_s("%d %d", &n, &k) != 2) return 0;
//
//    static int a[200000];
//    for (int i = 0; i < n; ++i) scanf_s("%d", &a[i]);
//
//    qsort(a, n, sizeof(int), cmp_int);
//
//    int left = 0;
//    int maxCnt = 0;
//
//    for (int right = 0; right < n; ++right)
//    {
//        while (a[right] - a[left] > k) ++left;
//        int cnt = right - left + 1;
//        if (cnt > maxCnt) maxCnt = cnt;
//    }
//    printf("%d\n", maxCnt);
//    return 0;
//}

#include <stdio.h>
#include <stdlib.h>

#define MAXN 100000

int A[MAXN];

int cmp_int(const void* p, const void* q)
{
    int a = *(const int*)p;
    int b = *(const int*)q;
    return (a > b) - (a < b);
}

int main(void) 
{
    int N, M;
    if (scanf_s("%d %d", &N, &M) != 2) return 0;
    for (int i = 0; i < N; ++i) scanf_s("%d", &A[i]);

    qsort(A, N, sizeof(int), cmp_int);

    long long cur = 0;
    for (int i = 1; i < M; ++i)
    {
        long long d = (long long)A[i] * A[i] - (long long)A[i - 1] * A[i - 1];
        if (d < 0) d = -d;
        cur += d;
    }
    long long minL = cur;

    for (int i = M; i < N; ++i)
    {
        long long out = (long long)A[i - M + 1] * A[i - M + 1]
            - (long long)A[i - M] * A[i - M];
        if (out < 0) out = -out;
        cur -= out;

        long long in = (long long)A[i] * A[i]
            - (long long)A[i - 1] * A[i - 1];
        if (in < 0) in = -in;
        cur += in;

        if (cur < minL) minL = cur;
    }

    printf("%lld\n", minL);
    return 0;
}